package com.hongxun.design.adapters;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hongxun.design.R;
import com.hongxun.design.bean.MaterialBean;
import com.hongxun.design.constant.Constant;
import com.hongxun.design.listeners.MClickListener;
import com.hongxun.design.mytools.MString;

import java.util.List;

/**
 * Created by zerdoor_pc on 2016/5/24.
 */
public class MaterialDetailAdapter extends BaseAdapter {
    List<MaterialBean> data;
    Context context;
    LayoutInflater inflater;
    int type;

    public MaterialDetailAdapter(List<MaterialBean> data, Context context, int type) {
        this.data = data;
        this.context = context;
        this.type = type;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Holler holler;
        MaterialBean bean = data.get(i);
        if (view == null) {
            view = inflater.inflate(R.layout.listview_item_materialdetail2, null);
            holler = new Holler(view);
            view.setTag(holler);
        } else {
            holler = (Holler) view.getTag();
        }
        holler.title.setText(bean.getName());
        holler.model.setText(bean.getSpec());
        holler.num.setText(bean.getNum());
        holler.company.setText(bean.getUnit());

        if (bean.getBig_category() != null && bean.getBig_category().equals("1")) {
            holler.m2iv.setVisibility(View.VISIBLE);
            if (bean.getMiddle_category() != null && bean.getMiddle_category().equals("1")) {
                holler.m2iv.setImageResource(R.mipmap.checkon);
            } else {
                holler.m2iv.setImageResource(R.mipmap.checkoff);
            }
        } else {
            holler.m2iv.setVisibility(View.GONE);
            holler.m2iv.setOnClickListener(null);
        }
        if (TextUtils.isEmpty(bean.getObj_type())) {
            holler.type.setText("材料");
            holler.type.setBackgroundResource(R.drawable.circular_green_2);
        } else {
            if (bean.getObj_type().equals("0")) {//材料
                holler.type.setText("材料");
                holler.type.setBackgroundResource(R.drawable.circular_green_2);
            } else {//模块
                holler.type.setText("模块");
                holler.type.setBackgroundResource(R.drawable.circular_yellow_2);
            }
        }
        if (type == Constant.EDIT) {
            if (bean.getUse_type() != null && bean.getUse_type().equals(MString.NEW)) {
                holler.old.setText(MString.YES);
                holler.old.setBackgroundResource(R.drawable.circular_red_2);
            } else {
                holler.old.setText(MString.NO);
                holler.old.setBackgroundResource(R.drawable.circular_gray_2);
            }
            holler.old_item.setVisibility(View.VISIBLE);
        } else {
            holler.old_item.setVisibility(View.GONE);
        }
        return view;
    }

    class Holler {
        public TextView type, title, model, num, company, old;
        RelativeLayout old_item;
        private ImageView m2iv;

        Holler(View view) {
            type = (TextView) view.findViewById(R.id.materialdetail_type);
            title = (TextView) view.findViewById(R.id.materialdetail_title);
            model = (TextView) view.findViewById(R.id.materialdetail_model);
            num = (TextView) view.findViewById(R.id.materialdetail_num);
            company = (TextView) view.findViewById(R.id.materialdetail_company);
            old = (TextView) view.findViewById(R.id.materialdetail_old);
            old_item = (RelativeLayout) view.findViewById(R.id.materialdetail_old_item);
            m2iv = (ImageView) view.findViewById(R.id.m2_iv);
        }
    }
}
